package cn.glinks.liuhe.activity;

import android.os.Bundle;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.glinks.liuhe.R;
import cn.glinks.liuhe.api.ApiConstants;
import cn.glinks.liuhe.api.ApiRequest;
import cn.glinks.liuhe.api.jsonbean.PersonalInterest;
import cn.glinks.liuhe.api.jsonbean.UserInfo;
import cn.glinks.liuhe.app.AppApplication;
import cn.glinks.liuhe.app.AppConstant;
import cn.glinks.liuhe.base.BaseActivity;
import cn.glinks.liuhe.baserx.RxSubscriber;
import cn.glinks.liuhe.model.Interest;
import cn.glinks.liuhe.utils.ActTransAnimUtils;
import cn.glinks.liuhe.utils.GsonUtils;
import cn.glinks.liuhe.utils.SPUtils;
import cn.glinks.liuhe.utils.ToastUtils;

/**
 * Des:填写爱好页面
 *
 * @author MarsBlade of Cyh
 * @created 2017-03-21 14:43
 */
public class FillInterestActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView mTitleText;

    @BindView(R.id.et_hobby)// 爱好
    EditText mHobbyEdit;

    @BindView(R.id.et_sport)// 运动
    EditText mSportEdit;

    @BindView(R.id.et_travel)// 旅游
    EditText mTravelEdit;

    @BindView(R.id.et_intercourse)// 交往
    EditText mIntercourseEdit;

    @BindView(R.id.et_living)// 居住
    EditText mLivingEdit;

    @BindView(R.id.et_character)// 性格
    EditText mCharacterEdit;

    // 变量
    private UserInfo mUserInfo;
    private String mHobbyStr;
    private String mSportStr;
    private String mTravelStr;
    private String mIntercourseStr;
    private String mLivingStr;
    private String mCharacterStr;

    @Override
    public int getLayoutId() {
        return R.layout.activity_fill_interest;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        // 加载用户信息
        mUserInfo = ((AppApplication)getApplicationContext()).getLoginUser();
        // 设置为透明状态栏
        SetTranslanteBar();
        // 初始化标题
        mTitleText.setText("爱好");
        // 初始化显示数据
        ShowData();
    }

    @Override
    protected void onResume() {
        super.onResume();

        boolean isChanged = SPUtils.getSharedBooleanData(this, AppConstant.IS_INTEREST_CHANGED);
        if (isChanged) {
            // 更新显示数据
            ShowData();
        }
    }
    
    /**
     * Des:获取选中数据
     * @param list 解析二级爱好的列表
     * @param separator 分隔符
     * @return
     */
    private String getCheckedData(List<Interest> list, String separator) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            Interest interest = list.get(i);
            if (interest.isChecked) {
                sb.append(interest.label).append(separator);
            }
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length()-1);
        }

        return sb.toString();
    }

    /**
     * Des:加载选中数据
     * @param editText 要加载的EditText对象
     * @param data 数据
     * @return
     */
    private void loadCheckedData(EditText editText, String data) {
        editText.setText(data);
        if (editText.getLineCount() > 1) {
            editText.setGravity(Gravity.LEFT);
        } else {
            editText.setGravity(Gravity.RIGHT);
        }
    }

    /**
     * Des:初始化显示数据
     */
    private void ShowData() {
        loadCheckedData(mHobbyEdit, getCheckedData(((AppApplication)getApplicationContext()).getHobbyList(), "、"));
        loadCheckedData(mSportEdit, getCheckedData(((AppApplication)getApplicationContext()).getSportList(), "、"));
        loadCheckedData(mTravelEdit, getCheckedData(((AppApplication)getApplicationContext()).getTravelList(), "、"));
        loadCheckedData(mIntercourseEdit, getCheckedData(((AppApplication)getApplicationContext()).getIntercourseList(), "、"));
        loadCheckedData(mLivingEdit, getCheckedData(((AppApplication)getApplicationContext()).getLivingList(), "、"));
        loadCheckedData(mCharacterEdit, getCheckedData(((AppApplication)getApplicationContext()).getCharacterList(), "、"));
    }

    /*--------------------------------------------------------------------------------
     *                          点击事件接口实现
     ---------------------------------------------------------------------------------*/
    // 选择爱好
    @OnClick({R.id.et_hobby, R.id.iv_hobby})
    public void clickSelectEntertainment() {
        Bundle bundle = new Bundle();
        bundle.putString(AppConstant.BUNDLE_INTEREST_TAG_NAME, AppConstant.INTEREST_HOBBY);
        ActTransAnimUtils.startActionSlideRightToLeft(FillInterestActivity.this, InterestActivity.class, bundle);
    }

    // 选择运动
    @OnClick({R.id.et_sport, R.id.iv_sport})
    public void clickSelectDiet() {
        Bundle bundle = new Bundle();
        bundle.putString(AppConstant.BUNDLE_INTEREST_TAG_NAME, AppConstant.INTEREST_SPORT);
        ActTransAnimUtils.startActionSlideRightToLeft(FillInterestActivity.this, InterestActivity.class, bundle);
    }

    // 选择旅游
    @OnClick({R.id.et_travel, R.id.iv_travel})
    public void clickSelectTravel() {
        Bundle bundle = new Bundle();
        bundle.putString(AppConstant.BUNDLE_INTEREST_TAG_NAME, AppConstant.INTEREST_TRAVEL);
        ActTransAnimUtils.startActionSlideRightToLeft(FillInterestActivity.this, InterestActivity.class, bundle);
    }

    // 选择交往
    @OnClick({R.id.et_intercourse, R.id.iv_intercourse})
    public void clickSelectIntercourse() {
        Bundle bundle = new Bundle();
        bundle.putString(AppConstant.BUNDLE_INTEREST_TAG_NAME, AppConstant.INTEREST_INTERCOURSE);
        ActTransAnimUtils.startActionSlideRightToLeft(FillInterestActivity.this, InterestActivity.class, bundle);
    }

    // 选择居住
    @OnClick({R.id.et_living, R.id.iv_living})
    public void clickSelectLiving() {
        Bundle bundle = new Bundle();
        bundle.putString(AppConstant.BUNDLE_INTEREST_TAG_NAME, AppConstant.INTEREST_LIVING);
        ActTransAnimUtils.startActionSlideRightToLeft(FillInterestActivity.this, InterestActivity.class, bundle);
    }

    // 选择性格
    @OnClick({R.id.et_character, R.id.iv_character})
    public void clickSelectCharacter() {
        Bundle bundle = new Bundle();
        bundle.putString(AppConstant.BUNDLE_INTEREST_TAG_NAME, AppConstant.INTEREST_CHARACTER);
        ActTransAnimUtils.startActionSlideRightToLeft(FillInterestActivity.this, InterestActivity.class, bundle);
    }

    // 提交个人资料
    @OnClick(R.id.btn_confirm)
    public void clickCommitInfo() {
        // 获取数据
        mHobbyStr = mHobbyEdit.getText().toString().trim().replace("、", "|");
        mSportStr = mSportEdit.getText().toString().trim().replace("、", "|");
        mTravelStr = mTravelEdit.getText().toString().trim().replace("、", "|");
        mIntercourseStr = mIntercourseEdit.getText().toString().trim().replace("、", "|");
        mLivingStr = mLivingEdit.getText().toString().trim().replace("、", "|");
        mCharacterStr = mCharacterEdit.getText().toString().trim().replace("、", "|");
        // 检查数据有效性
        if (mHobbyStr.isEmpty()) {
            ToastUtils.showShort("请选择爱好");
            return;
        }
        if (mSportStr.isEmpty()) {
            ToastUtils.showShort("请选择运动");
            return;
        }
        if (mTravelStr.isEmpty()) {
            ToastUtils.showShort("请选择旅游");
            return;
        }
        if (mIntercourseStr.isEmpty()) {
            ToastUtils.showShort("请选择交往");
            return;
        }
        if (mLivingStr.isEmpty()) {
            ToastUtils.showShort("请选择居住");
            return;
        }
        if (mCharacterStr.isEmpty()) {
            ToastUtils.showShort("请选择性格");
            return;
        }

        // 打包数据
        PersonalInterest interest = new PersonalInterest();
        interest.setEntertainment(mHobbyStr);
        interest.setDiet(mSportStr);
        interest.setTravel(mTravelStr);
        interest.setIntercourse(mIntercourseStr);
        interest.setLiving(mLivingStr);
        interest.setCharacter(mCharacterStr);

        // 发送网络请求
        mRxManager.add(ApiRequest
                .requestCommitInterest(mUserInfo.getJwt(), interest)
                .subscribe(new RxSubscriber<String>(mContext) {
                    @Override
                    protected void _onNext(String msg) {
                        // 响应处理
                        if (GsonUtils.getIntValue(msg, "error") == 0) {
                            ToastUtils.showShort("提交爱好成功");
                            // 跳转到测评主页面
                            ActTransAnimUtils.startActionSlideRightToLeft(FillInterestActivity.this, MainActivity.class);
                            finish();
                        } else {
                            String reason = GsonUtils.getValue(msg, "reason");
                            ApiConstants.showError(reason);
                        }
                    }

                    @Override
                    protected void _onError(String message) {
                        // TODO:处理失败响应
                        ToastUtils.showShort("提交爱好失败:" + message);
                    }
                }));
    }

}
